
from collections import Counter

class Solution:

    def findAnagrams(self, s, p):

        if len(p) > len(s):
            return []

        # 设置滑动窗口的滑动范围
        window = len(p)
        ans = []

        target = Counter(p)
        init = Counter(s[0: window])
        # 注意，遍历的时候需要考虑复用性
        for i in range(len(s) - window + 1):
            # print(init)
            # if s[i: window] == p:
            #     continue
            # temp = s[i: window + i]
            if i > 0:
                init[s[i - 1]] -= 1
                init[s[i + window - 1]] += 1
                if init[s[i - 1]] == 0:
                    init.pop(s[i - 1])
            # print(temp)
            if target == init:
                # print(set(p))
                # print(set(temp))
                # print(s[i: window])
                ans.append(i)
                # print(s)

            # print(i)

        return ans
    

if __name__ == "__main__":

    s = "cbaebabacd"
    p = "abc"
    sol = Solution()
    ans = sol.findAnagrams(s , p)
    print(ans)